#!/bin/bash

if [ $# -ne 6 ]
then
	echo "Usage: $0 minimum maximum number_of_tests accepted_mean_result epsilon problem_size"
	echo "epsilon - is the MINIMUM interval between the maximum and minimum, it is a measure of precision"
	echo "minimum - minimum limit of the variable to be considered (for example number of evaluations)"
	echo "maximum - maximum limit of the variable to be considered (for example number of evaluations)"
	echo "number_of_tests - number of tests repeated to compute the mean result"
	echo "problem_size - size of the problem"
	echo " "
	echo "Example - for a deceptive problem with problem size = 40, in which we wish to find the minimum population"
	echo "in which all results must have k-1 BBs, the following line would represent"
	echo "./bissection 100 200000 30 9.75 50 40"
	exit
fi


minimum=$1;
maximum=$2;
number_of_tests=$3;
accepted_result=$4;
epsilon=$5;
problem_size=$6
try=`echo "scale=2; ($minimum + $maximum) / 2" | bc`

echo "try: $try"

mean_value=0.0;
real_time=0.0;

#current_epsilon=`echo "scale=2; ($try - $maximum)" | bc`
current_epsilon=`echo "($try - $maximum)" | bc`

#absolute value
current_epsilon=`echo $current_epsilon| awk '{ print ($1 >= 0) ? $1 : 0 - $1}'`

echo "cur" $current_epsilon  "ep" $epsilon
compare_epsilon=`echo "$current_epsilon > $epsilon"|bc`

while [ $compare_epsilon -eq 1 ]
do

	mean_value=0
	not_accepted=0

	for (( i=0 ; i < $number_of_tests ; i++ ))
	do
		time_before=`date +%s`
		./o $try $problem_size | tail -n 2 > result
		time_after=`date +%s`
		real_time=`echo $real_time + $time_after - $time_before | bc`

		var=`cat result | cut -d ' ' -f 2|tail -n 1`
		array[$i]=$var
		mean_value=`echo $mean_value + $var | bc`
		echo "test" $i  "result" $var

		compare_result=`echo "$var < $accepted_result"|bc`
		if [ $compare_result -eq 1 ] 
		then
			echo "result not accepted"
			not_accepted=1;
			break;
		fi

		sleep 1
	done

	mean_value=`echo "scale=2; $mean_value / $number_of_tests" | bc`
	real_time=`echo "scale=2; $real_time / $number_of_tests" | bc`

#	echo "mean value" $mean_value  "accepted " $accepted_result

#	compare_result=`echo "$mean_value >= $accepted_result"|bc`

	if [ $not_accepted -eq 0 ]
	then
		maximum=$try;
		try=`echo "scale=2; ($minimum + $maximum) / 2" | bc`
		echo "result accepted"
		echo "try: $try  min $minimum max $maximum"
	else
		minimum=$try;
		try=`echo "scale=2; ($minimum + $maximum) / 2" | bc`
		echo "result not accepted"
		echo "try: $try  min $minimum max $maximum"
		
	fi

	current_epsilon=`echo "scale=2; ($minimum - $maximum)" | bc`
	#absolute value
	current_epsilon=`echo $current_epsilon| awk '{ print ($1 >= 0) ? $1 : 0 - $1}'`

	compare_epsilon=`echo "$current_epsilon > $epsilon"|bc`

done

#mean_value=`echo scale=4; ${mean_value} | bc`

#calculate standard_deviation
#standard_deviation=0.0;
#i=0
#echo "dev: $standard_deviation"
#while [ $i -lt ${#array[*]} ]; 
#do
#	standard_deviation=`echo "scale=5; $standard_deviation + (${array[$i]} - $mean_value)^2" | bc`
#	let i++
#done
#standard_deviation=`echo "scale=2; sqrt($standard_deviation/$number_of_tests)" | bc`


echo "Results from $number_of_tests execution(s):"
echo "	average solution: $mean_value"
#echo "	standard deviation: $standard_deviation"
echo "	average time: $real_time seconds"
echo "  minimum variable in bissection: $maximum "
